package com.duanjw.bank.rate.handler;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.duanjw.bank.rate.core.BankRate;
import com.duanjw.bank.rate.core.BankRateHandler;
import com.duanjw.bank.rate.core.DateFormatUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 招商银行
 *
 * @author duanjw
 * @date 2024/4/1
 */
public class ZhaoShangBanRateHandler implements BankRateHandler {
    @Override
    public BankRate getBankRate() throws IOException {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"); // 仿真
        Connection connect = Jsoup.connect("https://fin.paas.cmbchina.com/fininfo/api/interestRate/getInterestRate");
        String responseBody = connect.headers(headers).ignoreContentType(true).execute().body();

        JSONObject responseJson = new JSONObject(responseBody).getJSONObject("body");
        JSONArray interestRateVOS = responseJson.getJSONArray("interestRateVOS");
        String kaiShiZhiXingRiQi = responseJson.getStr("rbmRateDate");

        String huoQiCunKuanLiLv = interestRateVOS.get(0, JSONObject.class).getStr("currency_10");

        String zhengCunZhengQu3YueLiLv = interestRateVOS.get(4, JSONObject.class).getStr("currency_10");
        String zhengCunZhengQu6YueLiLv = interestRateVOS.get(5, JSONObject.class).getStr("currency_10");
        String zhengCunZhengQu12YueLiLv = interestRateVOS.get(6, JSONObject.class).getStr("currency_10");
        String zhengCunZhengQu24YueLiLv = interestRateVOS.get(7, JSONObject.class).getStr("currency_10");
        String zhengCunZhengQu36YueLiLv = interestRateVOS.get(8, JSONObject.class).getStr("currency_10");
        String zhengCunZhengQu60YueLiLv = interestRateVOS.get(9, JSONObject.class).getStr("currency_10");

        String lingCunZhengQu12YueLiLv = interestRateVOS.get(10, JSONObject.class).getStr("currency_10");
        String lingCunZhengQu36YueLiLv = interestRateVOS.get(11, JSONObject.class).getStr("currency_10");
        String lingCunZhengQu60YueLiLv = interestRateVOS.get(12, JSONObject.class).getStr("currency_10");

        String dingHuoLiangBian3YueLiLv = null;
        String dingHuoLiangBian6YueLiLv = null;
        String dingHuoLiangBian12YueLiLv = null;

        String tongZhiCunKuan1TianLiLv = interestRateVOS.get(1, JSONObject.class).getStr("currency_10");
        String tongZhiCunKuan7TianLiLv = interestRateVOS.get(2, JSONObject.class).getStr("currency_10");

        String xieDingCunKuanLiLv = null;

        return new BankRate(DateFormatUtil.format3(kaiShiZhiXingRiQi), huoQiCunKuanLiLv, zhengCunZhengQu3YueLiLv, zhengCunZhengQu6YueLiLv, zhengCunZhengQu12YueLiLv, zhengCunZhengQu24YueLiLv, zhengCunZhengQu36YueLiLv, zhengCunZhengQu60YueLiLv, lingCunZhengQu12YueLiLv, lingCunZhengQu36YueLiLv, lingCunZhengQu60YueLiLv, dingHuoLiangBian3YueLiLv, dingHuoLiangBian6YueLiLv, dingHuoLiangBian12YueLiLv, tongZhiCunKuan1TianLiLv, tongZhiCunKuan7TianLiLv, xieDingCunKuanLiLv);
    }

    @Override
    public String getBankName() {
        return "招商银行";
    }
}
